//
//  HJStatus.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/25.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit

class HJStatus: NSObject {
    
    ///微博创建时间
    var created_at:	String?
    
    ///微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?{
        
        didSet{
            
            source = source?.sinaSource()
        }
    }
    
    /// 微博配图
    var pic_urls: [[String: AnyObject]]?{
        
        didSet{
            ///如果没有图片直接返回
            if pic_urls?.count == 0{
                
                return
            }
            ///有图片
            picturesURL = [NSURL]()
            
            ///遍历pic_urls
            for dict in pic_urls!{
            ///
                guard let urlSting = dict["thumbnail_pic"]as? String else{
                    
                    HJPrint(msg: "没有值")
                    continue
                }
                guard let url = NSURL(string: urlSting) else{
                    
                    HJPrint(msg: "url没有值")
                    break
                }
                
                picturesURL?.append(url)
            }
            
        }
        
        
    }
    
    
    
    ///微博配图 存储的是URL地址
    var picturesURL: [NSURL]?
    
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数
    var attitudes_count: Int = 0
    
    ///被转发的微博
    var retweeted_status: HJStatus?
    
    
    ///user用户
    var user: HJUser?
    
    ///需要将user的key拦截下来
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user"{
            //获取value
            
            if let dict = value as? [String : AnyObject]{
                //字典转模型
                user = HJUser(dict: dict)
            }
            
            return
        }
        
        if key == "retweeted_status"{
            
            if let dict = value as? [String : AnyObject]{
                
                retweeted_status = HJStatus(dict: dict)
            }
        
           return
        }
        
        
        
        super.setValue(value, forKey: key)
    }
    
    
    ///字典转模型
    init(dict:[String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    ///对象打印
    
    override var description: String{
        
        let keys = ["created_at","id","text","source","pic_urls","reposts_count","comments_count","attitudes_count"]
        
        return "\n \t微博模型\(dictionaryWithValuesForKeys(keys).description)"
    }
    
    
}
